﻿using System.Windows.Media;
using System.Reflection;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Wpf.Ui.Abstractions.Controls;
using Wpf.Ui.Appearance;

namespace DesignPattern.ViewModels.Pages
{
    public partial class SettingsViewModel : ObservableObject, INavigationAware
    {
        private bool _isInitialized = false;
        [ObservableProperty] private string _appVersion = string.Empty;
        [ObservableProperty] private ApplicationTheme _currentTheme = ApplicationTheme.Unknown;
        //MVVMToolkit的分部方法，在CurrentTheme属性改变的时候执行
        partial void OnCurrentThemeChanged(ApplicationTheme oldValue, ApplicationTheme newValue)
        {
            ApplicationThemeManager.Apply(newValue);
        }
        //实现INavigationAware接口
        public Task OnNavigatedToAsync()
        {
            if (!_isInitialized)
                InitializeViewModel();
            return Task.CompletedTask;
        }
        public Task OnNavigatedFromAsync() => Task.CompletedTask;
        //导航到页面之后执行
        private void InitializeViewModel()
        {
            CurrentTheme = ApplicationThemeManager.GetAppTheme();
            AppVersion = $"{GetAssemblyVersion()}";
            ApplicationThemeManager.Changed += OnThemeChanged;
            _isInitialized = true;
        }

        //动态改变主题
        private void OnThemeChanged(ApplicationTheme currentTheme, Color systemAccent)
        {
            if (CurrentTheme != currentTheme)
            {
                CurrentTheme = currentTheme;
            }
        }
        //App的版本号
        private static string GetAssemblyVersion()
        {
            return Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? string.Empty;
        }

        //主题切换
    }
}